package com.hitqz.robot.biz.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.Objects;
import com.hitqz.robot.api.business.enums.BrandEnum;
import com.hitqz.robot.api.business.enums.EquipmentEnum;
import com.hitqz.robot.api.business.enums.RobotOtherCommandEnum;
import com.hitqz.robot.api.common.entity.PatrolPose;
import com.hitqz.robot.api.common.entity.RobotEquipment;
import com.hitqz.robot.biz.mapper.PatrolPoseMapper;
import com.hitqz.robot.biz.service.PatrolPoseService;
import com.hitqz.robot.biz.service.RobotEquipmentService;
import com.hitqz.robot.camera.EquipmentFactory;
import com.hitqz.robot.camera.IEquipmentService;
import com.hitqz.robot.camera.configure.EquipmentCmdEnum;
import com.hitqz.robot.camera.dto.EquipmentParamsDto;
import com.hitqz.robot.camera.dto.QueryParamsDto;
import com.hitqz.robot.common.core.util.R;
import com.hitqz.robot.driver.parent.RobotDriverService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author wengyx
 * @date 2024/8/7
 */
@Service
@AllArgsConstructor
public class PatrolPoseServiceIml extends ServiceImpl<PatrolPoseMapper, PatrolPose> implements PatrolPoseService {

    private final RobotEquipmentService robotEquipmentService;
    private final EquipmentFactory equipmentFactory;
    final private RobotDriverService robotDriverService;

    @Override
    public R executeAsync(PatrolPose patrolPose, Integer cmd) {
        RobotEquipment equipment = robotEquipmentService.getById(patrolPose.getEquipmentId());
        if (Objects.equal(equipment.getBrand(), BrandEnum.ROBOT.name())) {
            //通过机器人驱动控制的外设
            Integer type = equipment.getType();
            if (Objects.equal(type, EquipmentEnum.ROBOT.getType())) {
                if (cmd == 0){
                    robotDriverService.executeOtherCommand(RobotOtherCommandEnum.SET_JACKING_ORIGIN, null);
                }
                if(cmd ==1){
                    List<EquipmentParamsDto> paramsDtos = JSONArray.parseArray(patrolPose.getJson(), EquipmentParamsDto.class);
                    robotDriverService.executeOtherCommand(RobotOtherCommandEnum.SET_JACKING, JSONArray.toJSONString(paramsDtos));
                }
            }
        }else {
            //真的外设
            IEquipmentService service = equipmentFactory.getServiceByBrand(equipment.getBrand());
            if (service==null){
                return R.failed("找不到"+equipment.getName()+"驱动");
            }
            if (cmd == 0) {
                // 回正
                QueryParamsDto queryParamsDto = new QueryParamsDto();
                queryParamsDto.setPassword(equipment.getPassword());
                queryParamsDto.setUsername(equipment.getUsername());
                queryParamsDto.setPort(equipment.getPort());
                queryParamsDto.setChannel(equipment.getChannel());
                queryParamsDto.setIp(equipment.getIp());
                service.setEquipmentParams(queryParamsDto,
                        service.getDeviceParamsData(queryParamsDto, EquipmentCmdEnum.GET_ORIGIN_PARAMS), true);
            }
            if (cmd == 1) {
                //调用
                List<EquipmentParamsDto> paramsDtos = JSONArray.parseArray(patrolPose.getJson(), EquipmentParamsDto.class);
                QueryParamsDto queryParamsDto = new QueryParamsDto();
                queryParamsDto.setPassword(equipment.getPassword());
                queryParamsDto.setUsername(equipment.getUsername());
                queryParamsDto.setPort(equipment.getPort());
                queryParamsDto.setChannel(equipment.getChannel());
                queryParamsDto.setIp(equipment.getIp());
                service.setEquipmentParams(queryParamsDto, paramsDtos , true);
            }
        }
        return R.ok();
    }
}
